
// 数组转树
function arrayToTree(items) {
    const result = [];
    const itemMap = {};
    for (const item of items) {
        const id = item.id;
        const pid = item.pid;
        if (itemMap[id]) {
            itemMap[id] = { ...item, ...itemMap[id] }
        } else {
            itemMap[id] = { ...item, children: [] }
        }

        const treeItem = itemMap[id];
        if (pid === 0) {
            result.push(treeItem)
        } else {
            if (!itemMap[pid]) {
                itemMap[pid] = { children: [] }
            }
            itemMap[pid].children.push(treeItem)
        }
        return result
    }
}


/**
 * 递归查找，获取children
 */
const getChildren = (data, result, pid) => {
    for (const item of data) {
        if (item.pid === pid) {
            const newItem = { ...item, children: [] };
            result.push(newItem);
            getChildren(data, newItem.children, item.id);
        }
    }
}

/**
* 转换方法
*/
const arrayToTree = (data, pid) => {
    const result = [];
    getChildren(data, result, pid)
    return result;
}





// 树转数组
function treeToArray() {

}

































const arrayToTree = (arr) => {
    return arr.filter((father) => {
        const branch = arr.filter((child) => {
            return child.pid === father.id;
        });
        // eslint-disable-next-line no-param-reassign
        if (branch.length) father.children = branch;
        return father.pid === 0;
    });
};
